package com.sx.controller;


import com.sx.domain.Course;
import com.sx.domain.CourseLesson;
import com.sx.domain.CourseSection;
import com.sx.domain.ResponseResult;
import com.sx.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLessonByCourseId(@RequestParam Integer courseId){

        try {
            List<CourseSection> list = courseContentService.findSectionAndLessonByCourseId(courseId);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", list);
            return result;
        }catch (Exception e){
            return new ResponseResult(false,500,"响应失败",e.getMessage());

        }


    }

    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(@RequestParam Integer courseId){
        Course courseByCourseId = courseContentService.findCourseByCourseId(courseId);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", courseByCourseId);
        return result;


    }
    
    // 保存或更新章节


    /**
     * 保存&修改章节信息
     */
    @RequestMapping(value = "/saveOrUpdateSection", method = RequestMethod.POST)
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection section) {
        /**
         * @RequestParam查询参数或表单数据
         * @RequestBody用于接收HTTP请求体中的数据（JSON格式）
         */
        
        // 添加对 sectionName 的非空校验
        if (section.getSectionName() == null || section.getSectionName().trim().isEmpty()) {
            return new ResponseResult(false, 400, "章节名称不能为空", null);
        }
        
        try {
            //判断携带id是修改操作否则是插入操作
            if(section.getId() == null){
                courseContentService.saveSection(section);
                return new ResponseResult(true,200,"响应成功",null);
            }else{
                courseContentService.updateSection(section);
                return new ResponseResult(true,200,"响应成功",null);
            }
        } catch (Exception e) {
            return new ResponseResult(false,500,"响应失败",e.getMessage());
        }
    }

    /**
     * 章节状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(@RequestParam int id,@RequestParam int status){
        try {
            courseContentService.updateSectionStatus(id,status);
            //封装最新的状态信息
            Map<String,Integer> map = new HashMap<>();
            map.put("status",status);
            ResponseResult result = new ResponseResult(true,200,"响应成功",map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 添加和修改课时信息
     */
    @RequestMapping("/saveOrUpdateLesson")

    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson lesson){
        try {
            if (lesson.getId() == null){
                courseContentService.AddcClasShourInformation(lesson);
                ResponseResult result = new ResponseResult(true,200,"保存成功",null);
                return result;
            }else {
                courseContentService.updateLesson(lesson);
                ResponseResult result = new ResponseResult(true,200,"修改成功",null);
                return result;
            }
        }catch (Exception e){
            return new ResponseResult(false,500,"操作失败: " + e.getMessage(),null);
        }


    }



}